package com.lx.yygh.service.impl;

import com.lx.yygh.service.SmsService;
import com.lx.yygh.util.SyncSmsUtil;
import com.lx.yygh.util.ValidateCodeUtils;
import com.lx.yygh.vo.msm.MsmVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author c先生
 * @date 2022年10月20日 下午 3:09:40
 * @describe
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Resource
    RedisTemplate redisTemplate;

    @Override
    public boolean sendMessage(String phone) {
        //生成验证码
        String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
        Map<String,String> map = new HashMap();
        map.put("code",code);
        //发送短信
        boolean flag = SyncSmsUtil.sendMessage(map, phone, "SMS_248905322");
        //把验证码存入Redis数据库中，并设置5分钟有效期
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        if(flag){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void send(MsmVo msmVo) {
        String phone = msmVo.getPhone();
        Map<String, Object> param = msmVo.getParam();
        Map<String,String> map = new HashMap<>();
        map.put("code", String.valueOf(param.get("code")));
        //发送短信
        boolean flag = SyncSmsUtil.sendMessage(map, phone, "SMS_248905322");

    }
}
